﻿var express = require('express');
var router = express.Router();
var connection = require('../backjs/node_mysql.js')

//查看委托单列表
router.post('/task_list', function (req, res) {
    //客户查看自己的
    if (req.body.requestType == 'client') {//改为session里面保存的权限判断？？？？
        var sql = "SELECT task_id,task_type,task_pname,task_state,task_start,task_fin,task_workNum FROM task where task_uid = ? order by task_id DESC";
        /*  if (req.body.sort) {
              sql += "where task_type like %xx% and task_start>=req.body.start and task_start<=req.body.fin"
  
          }*/
        var params = [req.body.task_uid];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                console.log("读取到了数据")
                res.send(rs);
                console.log(rs);
            }
        });
    }
    //业务部门
    else if (req.body.requestType == 'staff') {//改为session里面保存的权限判断？？？？
        var sql = 'select task_id,task_type,task_pname,task_state,task_start,task_fin,task_workNum,task_company,user_name,part_name' +
            ' from task left join user on user_id = task_uid left join part on part_id = task_department order by task_id DESC ';
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                console.log("读取到了数据")
                req.message = rs[0];
                res.send(rs);
                console.log(rs);
            }
        });
    }
    //检测主管
    else if (req.body.requestType == 'manager') {
        var sql = 'select tmp.*, user_name from (select task_id,task_state,task_pid,task_type,task_workNum,task_pname,task_company,task_tech,task_passTech,task_team,task_passTeam from task ' +
            'where task_department = ? )tmp left join project on tmp.task_pid = pj_id left join user on user_id = pj_uid order by task_id DESC ';
        var params = [req.body.user_part];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                console.log("读取到了数据")
                res.send(rs);
                console.log(rs);
            }
        });
    }
    else if (req.body.requestType == 'superManager'){
        var sql = 'select tmp.*, user_name from (select task_id,task_state,task_pid,task_type,task_workNum,task_pname,task_company,task_tech,task_passTech,task_team,task_passTeam from task ' +
            ')tmp left join project on tmp.task_pid = pj_id left join user on user_id = pj_uid order by task_id DESC ';
        var params = [req.body.user_part];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});


//委托单详情
router.post('/checkDepute', function (req, res) {
    //基本信息
    if (req.body.requestType == 'initBaseData') {
        var sql = 'select  task_company, task_check, task_pname, task_product, task_state, task_addr, task_workNum, task_opportunity, task_pdid, task_standard, task_type, ' +
            'task_position, task_time, task_heat, task_uid, task_phone, task_description, user_name from task left join user on task_uid = user_id where task_id = ?';
        var params = [req.body.task_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        })
    }
    //所有工件
    else if (req.body.requestType == 'initWorkPieceData') {
        var sql = 'select w_pipeNum, w_weldNum, w_pipeName, w_scale, w_spec, w_texture, w_type, w_weldM, w_weldPer, w_weldTime, w_test, w_qua ' +
            'from workpiece where w_tid = ?';
        var params = [req.body.w_tid];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                console.log('委托编号为 ' + req.body.task_id + ' 的工件信息为： ' + rs);
                res.send(rs);
            }
        })
    }
    //工件类型(分配工艺卡和小组的时候用到)
    else if (req.body.requestType == 'initWorkCountData') {
        var sql = 'select tmp.*,tech_id,tech_state,tp_team,team_name from (select wc_id,wc_pipeName,wc_spec,wc_texture,wc_type,wc_weldM from workcount ' +
            'where wc_tid = ?)tmp left join tech on tech_wcid = wc_id left join taskpaper on tp_wcid = wc_id left join team on team_id = tp_team';
        var params = [req.body.task_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});

//分配委托单同时预建派工单
router.post('/allot', function (req, res) {
    var sql = 'update task set task_department = ? where task_id = ?';
    var params = [req.body.task_department, req.body.task_id];
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        console.log("分配成功");
    });
    sql = 'insert into taskpaper (tp_wcid,tp_tid) select wc_id,wc_tid from workcount where wc_tid = ?';//写入工件类型号和委托单编号
    params = [req.body.task_id];
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        res.send(200);
        console.log("派工单新建成功");
    });

});


//新建委托单
router.get('/newDepute.html', function (req, res, next) {
    res.render('newDepute.html');
}).post('/newDepute', function (req, res) {
    //初始化
    if (req.body.requestType == 'init') {
        var sql = 'select pj_name from project where pj_id = ?';
        var params = [req.body.projectId];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                // console.log(rs)
                res.send(rs[0]);
            }
        });
    }
    else if (req.body.requestType == 'commitTaskTable') {
        // 新建
        var sql = 'insert into task (task_description,task_pid,task_pname,task_pdid,task_product,task_workNum,task_opportunity,' +
            'task_addr,task_standard,task_heat,task_position,task_phone,task_start,task_type,task_company,task_uid,task_level,task_check,task_time) values (?)';
        let params = [[req.body.task_description, req.body.task_pid, req.body.task_pname, req.body.task_pdid, req.body.task_product,
        req.body.task_workNum, req.body.task_opportunity, req.body.task_addr, req.body.task_standard, req.body.task_heat, req.body.task_position,
        req.body.task_phone, req.body.task_start, req.body.task_type, req.body.task_company, req.body.task_uid, req.body.task_level, req.body.task_check, req.body.task_time]];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
                console.log("委托单基本信息提交");
            }

        })
    }
    //工件信息写入
    else if (req.body.requestType == 'commitWorkpieceTable') {
        console.log(req.body);
        console.log(req.body.w_pipeName);
        let sql = 'insert into workpiece (w_pid, w_tid, w_pipeName, w_pipeNum, w_weldNum, w_weldPer, w_re, w_photoNum, w_spec, w_texture, w_scale, ' +
            'w_type, w_weldM, w_weldTime, w_test, w_qua) values ?';
        var num = req.body.num;
        var params = [];
        var vec;
        if (num == 1) {
            vec = [req.body.w_pid, req.body.w_tid, req.body.w_pipeName, req.body.w_pipeNum, req.body.w_weldNum, req.body.w_weldPer, req.body.w_re,
            req.body.w_photoNum, req.body.w_spec, req.body.w_texture, req.body.w_scale, req.body.w_type, req.body.w_weldM, req.body.w_weldTime,
            req.body.w_test, req.body.w_qua];
            params.push(vec);
        }
        else {
            for (var i = 0; i < num; i++) {
                vec = [req.body.w_pid, req.body.w_tid, req.body.w_pipeName[i], req.body.w_pipeNum[i], req.body.w_weldNum[i], req.body.w_weldPer[i], req.body.w_re[i],
                req.body.w_photoNum[i], req.body.w_spec[i], req.body.w_texture[i], req.body.w_scale[i], req.body.w_type[i], req.body.w_weldM[i], req.body.w_weldTime[i],
                req.body.w_test[i], req.body.w_qua[i]];
                params.push(vec);
            }
        }
        connection.query(sql, [params], function (err, rs) {
            if (err) {
                throw err;
            }
            res.send(200);
        });
    }
    //将本张委托单中的工件按：部件/管线名称、工件规格、材质、检测比例、焊接方法、检验方法、合格级别 分类并填入workcount
    else if (req.body.requestType == 'commitWorkCountTable') {
        console.log(req.body);
        let sql = 'insert into workcount(wc_tid,wc_pipeName,wc_spec,wc_texture,wc_scale,wc_weldM,wc_test,wc_qua,wc_type,wc_num) ' +
            'select w_tid,w_pipeName,w_spec,w_texture,w_scale,w_weldM,w_test,w_qua,w_type,COUNT(*) as w_num ' +
            'from workpiece where w_tid = ? GROUP BY w_tid,w_pipeName,w_spec,w_texture,w_scale,w_weldM,w_test,w_qua,w_type';
        let params = [req.body.w_tid];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                var sum;
                sql = 'select COUNT(*) as sum from workcount where wc_tid = ?';
                params = [req.body.w_tid];
                connection.query(sql, params, function (err, rs) {
                    if (err) {
                        throw err;
                    }
                    sum = rs[0].sum;
                    sql = 'update task set task_tech = ? ,task_team =? where task_id = ?';
                    params = [sum, sum, req.body.w_tid, sum];
                    connection.query(sql, params, function (err, rs) {
                        if (err) {
                            throw err;
                        }
                        res.send(200);
                    });
                });
            }
        });

    }
    //给工件打上类别标记
    else if (req.body.requestType == 'updateWCIdInWorkPiece') {
        let sql = 'update workpiece,workcount set w_wcid = wc_id where w_pipeName = wc_pipeName and w_spec = wc_spec and ' +
            'w_texture = wc_texture and w_scale = wc_scale and w_weldM = wc_weldM and w_test = wc_test and w_qua = wc_qua and w_tid = ? and w_tid = wc_tid';
        let params = [req.body.w_tid];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            res.send(200);
        });
    }
});

//工艺卡列表
router.post('/tecCard_list', function (req, res) {
    //审核部门
    if (req.body.requestType == 'check') {
        var sql = 'select tech_id,task_pname,task_product,task_type,tech_date,tech_state, ' +
            'tech_verify_date from tech left join task on task_id = tech_tid order by tech_id DESC';
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    //检测主管
    if (req.body.requestType == 'manager') {
        var sql = 'select tech_id,task_pname,task_type,task_product,wc_num,tech_date,tech_state,tech_verify_date from tech,task,workcount where ' +
            'task_department = ? and tech_tid = task_id and wc_id = tech_wcid order by tech_id DESC';
        var params = [req.body.user_part];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                console.log(rs);
                res.send(rs);
            }
        });
    }
    //超级管理员
    if (req.body.requestType == 'superManager') {
        var sql = 'select tech_id,task_pname,task_type,task_product,wc_num,tech_date,tech_state,tech_verify_date ' +
            'from tech,task,workcount where tech_tid = task_id and wc_id = tech_wcid order by tech_id DESC';
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});


//新建工艺卡(必要：工件类别编号！)
router.post('/newTecCard', function (req, res) {
    //基本信息，来自task
    if (req.body.requestType == 'initBaseInfoFromTask') {
        var sql = 'select task_pid, task_product, task_pname, task_opportunity,task_state, task_position, task_standard, task_level, task_check from task where task_id = ?';
        var params = [req.body.task_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs[0]);
            }
        });
    }
    //委托单下的工件类别列表
    else if (req.body.requestType == 'initListofWorkCount') {
        var sql = 'select * from workcount where wc_tid = ?';
        var params = [req.body.wc_tid];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    //根据工件类别编号取得基本信息，来自workcount
    else if (req.body.requestType == 'initBaseInfoFromWorkCount') {
        var sql = 'select wc_tid,wc_pipeName,wc_spec,wc_texture,wc_scale,wc_weldM,wc_test,wc_qua,wc_type,wc_num from workcount where wc_id = ?';
        var params = [req.body.wc_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs[0]);
            }
        });
    }
    //更改
    else if (req.body.requestType == 'update') {

    }
    //使用模板
    else if (req.body.requestType == 'initInfoFromTecMould') {
        var sql = 'select tech.*,user.user_name from tech,user where tech_id = ?,tech_verify = user_id';
        var params = [req.body.tech_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs[0]);
            }
        });
    }
    //提交工艺卡内容
    else if (req.body.requestType == 'commitTechTable') {
        var sql = 'insert into tech (tech_version, tech_pid, tech_tid, tech_wcid, tech_screen, ' +
            'tech_filmBrand, tech_devTime, tech_devT, tech_ray, tech_focus, tech_IQIV, tech_filmV, ' +
            'tech_devCondition, tech_liquor, tech_focalDis, tech_seg, tech_tranLen, tech_IQISen, ' +
            'tech_V, tech_time, tech_tranType,tech_date,tech_uid) values ?';
        var params = [[req.body.tech_version, req.body.tech_pid, req.body.tech_tid, req.body.tech_wcid,
        req.body.tech_screen, req.body.tech_filmBrand, req.body.tech_devTime, req.body.tech_devT,
        req.body.tech_ray, req.body.tech_focus, req.body.tech_IQIV, req.body.tech_filmV,
        req.body.tech_devCondition, req.body.tech_liquor, req.body.tech_focalDis, req.body.tech_seg,
        req.body.tech_tranLen, req.body.tech_IQISen, req.body.tech_V, req.body.tech_time,
        req.body.tech_tranType, req.body.tech_date, req.body.tech_uid]];
        connection.query(sql, [params], function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(200);
            }
        });
    }

});


//工艺卡列表
router.post('/listofTecCard', function (req, res) {
    if (req.body.requestType == 'check') {
        var sql = 'select tech_id,task_pname,task_product,task_type,tech_date,tech_state from tech,task where task_id  = tech_tid order by tech_id DESC';
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    else if (req.body.requestType == 'manager') {
        var sql = 'select tech_id,task_pname,task_product,task_type,tech_date,tech_state from tech,task ' +
            'where task_department = ? and tech_tid = task_id order by tech_id DESC';
        var params = [req.body.user_part];
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});
//查看工艺卡
router.get('/checkTecCard.html', function (req, res, next) {
    res.render('checkTecCard.html')
}).post('/checkTecCard', function (req, res) {//工艺卡信息查看
    if (req.body.requestType == 'initInfoFromTec') {
        var sql = 'select tech.*,task_product, task_level, task_opportunity, task_pname, task_state, task_position, task_standard,wc_pipeName,wc_spec, ' +
            'wc_texture,wc_scale,wc_weldM,wc_test,wc_qua,wc_type,wc_num from tech,task,workcount where tech_id = ? and tech_tid = task_id and tech_wcid = wc_id';
        var params = [req.body.tech_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    //审核工艺卡
    if (req.body.requestType == 'updateInfoFromTec') {
        var sql = 'update tech set tech_state = ?,tech_reason = ?,tech_verify = ?,tech_verify_date = ? where tech_id = ?';
        var params = [req.body.tech_state, req.body.reason, req.body.tech_verify, req.body.tech_verify_date,req.body.tech_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
        });
        if (req.body.tech_state == 1) {
            sql = 'update task set task_passTech = task_passTech + 1 where task_id = ?';
            params = [req.body.task_id];
            connection.query(sql, params, function (err, rs) {
                if (err) {
                    throw err;
                }
            });
        }
        res.send(200);
    }
    //修改工艺卡
    else if (req.body.requestType == 'updateTechTable') {
        var sql = 'update tech set tech_version = ? , tech_pid = ? , tech_tid = ? , tech_wcid = ? , tech_screen = ? ,tech_filmBrand = ? , ' +
            'tech_devTime = ? , tech_devT = ? , tech_ray = ? , tech_focus = ? , tech_IQIV = ? , tech_filmV = ? ,tech_devCondition = ? ,' +
            ' tech_liquor = ? , tech_focalDis = ? , tech_seg = ? , tech_tranLen = ? , tech_IQISen = ? ,tech_V = ? , tech_time = ? , ' +
            'tech_tranType = ? ,tech_date = ?, tech_state = 0 where tech_id = ?';
        var params = [req.body.tech_version, req.body.tech_pid, req.body.tech_tid, req.body.tech_wcid,
        req.body.tech_screen, req.body.tech_filmBrand, req.body.tech_devTime, req.body.tech_devT,
        req.body.tech_ray, req.body.tech_focus, req.body.tech_IQIV, req.body.tech_filmV,
        req.body.tech_devCondition, req.body.tech_liquor, req.body.tech_focalDis, req.body.tech_seg,
        req.body.tech_tranLen, req.body.tech_IQISen, req.body.tech_V, req.body.tech_time,
        req.body.tech_tranType, req.body.tech_date, req.body.tech_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(200);
            }
        });
    }
});


module.exports = router;